import Actor from "@/Actor";
import ObjectTemplateUtil from "@/ObjectTemplateUtil";
import DataBase from "@/DataBase";
import ActorAbility from "@/ActorAbility";
import UnitStateUtil from "@/UnitStateUtil";

/**
 * 演员单位
 * 演员单位的类型是公用的。
 *
 */
export default class ActorUnit extends Actor {
    unitTypeID: string

    constructor(actorTypeId: string, player: player, x: number, y: number) {
        super(actorTypeId)
        //单位模板 即用即还
        let templateType = this.getTemplateType();
        ObjectTemplateUtil._sl_init();
        if (ObjectTemplateUtil.templateInfo[templateType] == null) {
            //直接使用原生物编id 当模板
            this.unitTypeID = templateType;
        } else {
            this.unitTypeID = ObjectTemplateUtil.borrowTemplate(templateType);
            //单位模板 即用即还
            ObjectTemplateUtil.returnTemplate(templateType, this.unitTypeID);
        }
        if (this.unitTypeID == null || this.unitTypeID.length != 4) {
            log.errorWithTraceBack("物编模板id获取失败:" + tostring(templateType) + " -> " + tostring(this.unitTypeID))
            return;
        }
        //
        this.unit = CreateUnit(player, this.unitTypeID, x, y, 270)
        //
        DataBase.getUnitSolarData(this.unit)._SL_solarActorUnit = this;
        //
        this.init()
        this.refresh()
    }


    set<Key extends keyof AppActorType>(key: Key, value: AppActorType[Key]) {
        super.set(key, value);
        switch (key) {
            case "name":
                SetUnitName?.(this.unit, this.get("name"))
                SetUnitProperName?.(this.unit, this.get("name"))
                break;
            case "model":
                if (SetUnitModel) {
                    SetUnitModel(this.unit, this.get("model"))
                } else {
                    DzSetUnitModel(this.unit, this.get("model"))
                }
                break;
            case "missileModel":
                SetUnitMissileModel?.(this.unit, this.get("missileModel"))
                break;
            case "moveType":
                EXSetUnitMoveType(this.unit, this.get("moveType"))
                break;
            case "flyHeight":
                SetUnitFlyHeight(this.unit, this.get("flyHeight"), 0)
                break;
            case "maxLife":
                SetUnitState(this.unit, UNIT_STATE_MAX_LIFE, this.get("maxLife"))
                SetUnitState(this.unit, UNIT_STATE_LIFE, this.get("maxLife"))
                break;
            case "maxMana":
                SetUnitState(this.unit, UNIT_STATE_MAX_MANA, this.get("maxMana"))
                SetUnitState(this.unit, UNIT_STATE_MANA, this.get("maxMana"))
                break;
            case "range":
                SetUnitState(this.unit, UnitStateDamageRange, this.get("range"))
                break;
            case "damage":
                SetUnitState(this.unit, UnitStateDamageBase, this.get("damage"))
                break;
            case "damageCd":
                SetUnitState(this.unit, UnitStateDamageCool, this.get("damageCd"))
                break;
            case "def":
                SetUnitState(this.unit, UnitStateArmor, this.get("def"))
                break;
            case "moveSpeed":
                SetUnitMoveSpeed(this.unit, this.get("moveSpeed"))
                break;
        }
    }

    setXY(x: number, y: number) {
        super.setXY(x, y);
        SetUnitPosition(this.unit, x, y)
    }


    /**
     * 添加一个演员技能
     * @param actorAbilityTypeId
     * @param posNum 指定技能位置
     * @param startPos 若为指定技能位置则从技能起始位置开始找到后面的空位值
     */
    addActorAbility(actorAbilityTypeId: string, posNum?: number, startPos?: number): ActorAbility {
        let actorAbility = new ActorAbility(actorAbilityTypeId, this.unit, posNum, startPos);
        return actorAbility;

    }


    destroy() {
        super.destroy();
        RemoveUnit(this.unit)
    }

}